package com.bsy.videostation.adapter

import android.view.LayoutInflater
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.RoundedCornersTransformation
import com.bsy.framework.ext.data
import com.bsy.framework.utils.dpToPx
import com.bsy.videostation.constant.categoryPic
import com.bsy.videostation.constant.category_bg
import com.bsy.videostation.constant.category_text_bg
import com.bsy.videostation.model.res.VideoCategoryItem
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dg.videostation.R

/**
 * Created by since on 2024/5/28.
 * Copyright
 * 描述：
 */
class CategoryMainAdapter :
    BaseQuickAdapter<VideoCategoryItem, BaseViewHolder>(R.layout.item_category_main) {
    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val emptyView = LayoutInflater.from(context).inflate(R.layout.list_empty, recyclerView, false)
        setEmptyView(emptyView)
    }

    override fun convert(holder: BaseViewHolder, item: VideoCategoryItem) {
        val randomIndex = category_bg.indices.random()
        val randomCategoryBg = category_bg[randomIndex]
        holder.getView<ImageView>(R.id.iv_bg).load(randomCategoryBg) {
            transformations(RoundedCornersTransformation(dpToPx(10F), dpToPx(10F), 0F, 0F))
        }
        val randomCategoryTextBg = category_text_bg[randomIndex]
        holder.setImageResource(R.id.iv_text_bg, randomCategoryTextBg)

        val randomIndexCover = categoryPic.indices.random()
        val randomCategoryCover = categoryPic[randomIndexCover]
        val cover = if (item.cover.isNullOrEmpty()) randomCategoryCover else item.cover
        holder.getView<ImageView>(R.id.iv_cover).load(cover) {
            transformations(RoundedCornersTransformation(dpToPx(10F), dpToPx(10F), 0F, 0F))
        }
        holder.setText(R.id.tv_title, item.title.data())
        holder.setText(R.id.tv_des, item.desc.data())
    }
}